import api from './api';

class AIService {
  // Generate video from photos
  async generateVideoFromPhotos(photoData, options = {}) {
    try {
      const payload = {
        photos: photoData,
        options: {
          duration: options.duration || 10, // Default 10 seconds
          style: options.style || 'cinematic', // cinematic, animated, slideshow
          quality: options.quality || 'high', // low, medium, high
          music: options.music || 'auto', // auto, none, or specific music id
          transition: options.transition || 'fade', // fade, slide, zoom
          ...options
        }
      };
      
      // Use encryption for sensitive data
      const response = await api.post('/ai/generate-video', payload, true);
      
      return response;
    } catch (error) {
      throw new Error(error.message || 'Failed to generate video');
    }
  }

  // Get video generation status
  async getVideoGenerationStatus(taskId) {
    try {
      const response = await api.get(`/ai/video-status/${taskId}`);
      return response;
    } catch (error) {
      throw new Error(error.message || 'Failed to get video status');
    }
  }

  // Get generated video
  async getGeneratedVideo(taskId) {
    try {
      const response = await api.get(`/ai/video/${taskId}`);
      return response;
    } catch (error) {
      throw new Error(error.message || 'Failed to get video');
    }
  }

  // Delete generated video
  async deleteGeneratedVideo(taskId) {
    try {
      const response = await api.delete(`/ai/video/${taskId}`);
      return response;
    } catch (error) {
      throw new Error(error.message || 'Failed to delete video');
    }
  }

  // Get user's video history
  async getVideoHistory(page = 1, limit = 10) {
    try {
      const response = await api.get(`/ai/video-history?page=${page}&limit=${limit}`);
      return response;
    } catch (error) {
      throw new Error(error.message || 'Failed to get video history');
    }
  }

  // Get available video styles
  async getVideoStyles() {
    try {
      const response = await api.get('/ai/video-styles');
      return response;
    } catch (error) {
      throw new Error(error.message || 'Failed to get video styles');
    }
  }

  // Get available music options
  async getMusicOptions() {
    try {
      const response = await api.get('/ai/music-options');
      return response;
    } catch (error) {
      throw new Error(error.message || 'Failed to get music options');
    }
  }

  // Simulate video generation for demo purposes
  simulateVideoGeneration(photoData, options = {}) {
    return new Promise((resolve) => {
      // Simulate processing time based on photo count and quality
      const processingTime = (photoData.length * 1000) + (options.quality === 'high' ? 5000 : options.quality === 'medium' ? 3000 : 1000);
      
      setTimeout(() => {
        // Generate a mock video URL
        const videoId = `video_${Date.now()}`;
        const mockVideoUrl = `https://example.com/videos/${videoId}.mp4`;
        
        resolve({
          success: true,
          taskId: videoId,
          videoUrl: mockVideoUrl,
          thumbnail: `https://example.com/thumbnails/${videoId}.jpg`,
          duration: options.duration || 10,
          size: `${Math.floor(Math.random() * 50) + 10}MB`,
          createdAt: new Date().toISOString()
        });
      }, processingTime);
    });
  }

  // Simulate video generation status
  simulateVideoStatus(taskId, progress = 0) {
    return {
      taskId,
      status: progress < 100 ? 'processing' : 'completed',
      progress: Math.min(progress, 100),
      estimatedTimeRemaining: progress < 100 ? `${Math.floor((100 - progress) / 10)} seconds` : '0 seconds'
    };
  }
}

export default new AIService();